# 导入相关库和模块
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile, File, Request
from sqlalchemy.orm import Session
from database import SessionLocal
import sys
from admin.base_controller import BaseController
from config.logging import logger
from services.admin.upload_api_service import UploadApiService


# 定义获取数据库会话的依赖
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()


# 定义控制器类
class UploadApi(BaseController):
    def __init__(self):
        super().__init__()
        self.router = APIRouter()

    async def image_list(self, db: Session = Depends(get_db), page: int = Query(default=1, ge=1),
                        limit: int = Query(default=20, ge=1)):
        """
        菜单列表
        """
        service = UploadApiService(db)
        result = await service.image_list(page, limit)
        return self.success_response(data=result, message="success")

    async def upload_delete(self, uploadType: str, detailId: int = Query(...), db: Session=Depends(get_db)):
        """
        @description: 删除图片
        """
        service = UploadApiService(db)
        result = await service.upload_delete(detailId, uploadType)
        return self.success_response(data=result, message="success")

    async def upload_detail(self, uploadType: str, detailId: int = Query(...), db: Session=Depends(get_db)):
        """
        @description: 删除图片
        """
        service = UploadApiService(db)
        result = await service.upload_detail(detailId, uploadType)
        return self.success_response(data=result, message="success")

    async def upload_image(self, request: Request, file: UploadFile=File(...), db: Session=Depends(get_db)):
        """
        @description: 上传图片
        """
        service = UploadApiService(db, request.state.user)
        result = await service.upload_mode(file)
        return self.success_response(data=result, message="success")

    async def upload_file(self, request: Request, file: UploadFile=File(...), db: Session=Depends(get_db)):
        """
        @description: 上传文件,  excel, csv
        """
        service = UploadApiService(db, request.state.user)
        result = await service.upload_mode(file, "file")
        return self.success_response(data=result, message="success")



# 使用
upload_api = UploadApi()
router = upload_api.router
